﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class StatisticsController : Base
    {
        private readonly ArticleService _articleService;
        private readonly LoginUtil _LoginUtil;
        private readonly ArticleCateService _articleCateService;
        private readonly AdminUserService _adminUserService;
        public StatisticsController(
            ArticleService articleService,
            LoginUtil LoginUtil,
            ArticleCateService articleCateService,
            AdminUserService adminUserService
            )
        {
            _articleService = articleService;
            _LoginUtil = LoginUtil;
            _articleCateService = articleCateService;
            _adminUserService = adminUserService;
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public IActionResult Month()
        {
            return View();
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public IActionResult Year()
        {
            return View();
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public IActionResult Category()
        {
            return View();
        }

        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public IActionResult Index()
        {
            return View();
        }


        [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public async Task<IActionResult> ListAsync([FromQuery] StatisticsQuery query)
        {
            UserInfo adminUser = await _LoginUtil.UserInfoAsync();

            StatisticsQuery  statisticsQuery = new()
            {
                SearchType = query.SearchType,
                SearchCid = query.SearchCid,
                NickName = query.NickName,
                StartDate = query.StartDate,
                EndDate = query.EndDate,
            };

            IEnumerable<ArticleCate> articleCates = await _articleCateService.CategoryListAsync(adminUser.DeptId, adminUser.Gid,  (int)ModelTypeEnum.ModelType.信息报送管理);
            List<UserIdAndName> userIdAndName = await _adminUserService.GetNameAsync(0);
            var countList = await _articleService.StatisticsAsync(statisticsQuery);
            return Json(new { code = 0, data = countList , articleCates , userIdAndName });
        }


 [HttpGet]
        [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
        public async Task<IActionResult> MainAsync([FromQuery] StatisticsQuery query)
        {
            List<ArticleCount> forCateMonth = await _articleService.CountArticleForMonthAsync();
            List<ArticleCount> forUserYear = await _articleService.CountArticleForUserAsync(false);
            List<ArticleCount> forUserGroup = await _articleService.CountUserGroupYearAsync();
            return Json(new { status = 1, forCateMonth, forUserGroup,  forUserYear });
        }
    }
}
